package com.lanou3g.code0416;

import java.io.*;

/**
 * Created by dllo on 18/4/16.
 */
public class Main {

    public static void main(String[] args) {

    }

    public static void copyByBuffer(String srcPath, String desPath) {
        File srcFile = new File(srcPath);
        File desFile = new File(desPath);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            desFile.createNewFile();
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(desFile);
            /*
            bufferedinput/outputstream  创建的时候需要有
            别的字节流
             */
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            int pos = 0;
            byte[] buf = new byte[1024];
            while ((pos = bis.read(buf)) != -1) {
                bos.write(buf, 0, pos);
            }
            bos.flush();
            //在使用BufferedOutputStream的时候
            //需要在最后 加上一句bos.flush
            //无论Bos里面的数组  满没满
            //都将里面的数据写到硬盘中

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeAll(fis, fos, bis, bos);
            // fis,fos  可以写  可以不写  只需要关闭bis和bos
            //会在bis和bos得close方法中,自动关闭fis和fos
        }

    }
}
